package lib.self.adapter;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import lib.self.LogMgr;

/**
 * description: 封装了添加使用方法
 * author: Steven Xiao
 * date: 2017/5/15
 */
public abstract class FragmentPagerAdapterEx<T extends Fragment> extends FragmentPagerAdapter {
    private String TAG = getClass().getSimpleName();

    private List<T> mTs;

    public FragmentPagerAdapterEx(FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @Override
    public T getItem(int position) {
        if (mTs == null) {
            return null;
        }

        T t = null;
        try {
            t = mTs.get(position);
        } catch (Exception e) {
            LogMgr.e(TAG, e.toString());
        }

        return t;
    }

    @Override
    public int getCount() {
        return mTs == null ? 0 : mTs.size();
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }

    public void setData(List<T> data) {
        mTs = data;
    }

    public void add(T item) {
        if (item == null) {
            return;
        }

        if (mTs == null) {
            mTs = new ArrayList<T>();
        }
        mTs.add(item);
    }

    public void add(int position, T item) {
        if (item == null) {
            return;
        }

        if (mTs == null) {
            mTs = new ArrayList<T>();
        }
        mTs.add(position, item);
    }

    public void addAll(List<T> data) {
        if (data == null) {
            return;
        }

        if (mTs == null) {
            mTs = data;
        } else {
            mTs.addAll(data);
        }
    }

    public void addAll(int position, List<T> item) {
        if (mTs != null && item != null) {
            mTs.addAll(position, item);
        }
    }

    public List<T> getData() {
        return mTs;
    }

}
